from django.db import models
from django.contrib.auth.models import User
from public.models import CommonInfo
from appinput.models import App
from envx.models import Env


class DeployStatus(CommonInfo):
    """
    发布状态
    # 6个状态：新建（CREATE），编译(BUILD)，待发布(READY)，发布完成(FINISH)，失败(ERROR)，发布中(ING)
    """
    memo = models.CharField(max_length=1024, blank=True, verbose_name="备注")


IS_INC_TOT_CHOICES = (
    ("TOT", r"全量部署"),
    ("INC", r"增量部署"),
)

DEPLOY_TYPE_CHOICES = (
    ("deployall", r"发布所有"),
    ("deploypkg", r"发布程序"),
    ("deploycfg", r"发布配置"),
    ("rollback", r"回滚"),
)


class DeployPool(CommonInfo):
    """发布单"""
    name = models.CharField(max_length=100, blank=True, null=True, verbose_name="发布单编号")
    description = models.CharField(max_length=1024, blank=True, verbose_name="描述")
    app_name = models.ForeignKey(App, on_delete=models.CASCADE, related_name="deploy_app", verbose_name="APP应用")
    deploy_no = models.IntegerField(blank=True, null=True, default=0)
    branch_build = models.CharField(max_length=255, blank=True, null=True)
    jenkins_number = models.CharField(max_length=255, blank=True, null=True)
    code_number = models.CharField(max_length=255, blank=True, null=True)
    is_inc_tot = models.CharField(max_length=255, choices=IS_INC_TOT_CHOICES,
                                  blank=True, null=True, verbose_name="全量或增量部署")
    deploy_type = models.CharField(max_length=255, choices=DEPLOY_TYPE_CHOICES,
                                   blank=True, null=True, verbose_name="发布程序或配置")
    is_build = models.BooleanField(default=False, verbose_name="软件是否编译成功")
    create_user = models.ForeignKey(User, on_delete=models.CASCADE,
                                    related_name="deploy_create_user", verbose_name="创建用户")
    nginx_url = models.URLField(default=None, blank=True, null=True, verbose_name="Nginx URL")
    env_name = models.ForeignKey(Env, on_delete=models.CASCADE,related_name="deploy_env_name",
                                 blank=True, null=True, verbose_name="环境")
    deploy_status = models.ForeignKey(DeployStatus, on_delete=models.CASCADE, related_name="deploy_pool_status",
                                      blank=True, null=True, verbose_name="发布单状态")


class History(CommonInfo):
    """
    操作历史记录
    记得环境流转和软件发布中的操作，便于查看一些非法操作及未授权操作。
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="history_user",
                             blank=True, null=True, verbose_name="用户")
    app_name = models.ForeignKey(App, on_delete=models.CASCADE, related_name="history_app",
                                 blank=True, null=True, verbose_name="APP应用")
    env_name = models.ForeignKey(Env, on_delete=models.CASCADE, related_name="history_env_name",
                                 blank=True, null=True, verbose_name="环境")
    deploy_name = models.ForeignKey(DeployPool, on_delete=models.CASCADE, related_name="history_deploy",
                                    blank=True, null=True, verbose_name="发布单")
    do_type = models.CharField(max_length=32, blank=True, null=True, verbose_name="操作类型")
    content = models.CharField(max_length=1024, blank=True, null=True, verbose_name="操作内容")
